﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class TeamDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static TeamDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<TeamDTO> GetTeamList()
        {
            try
            {
                List<TeamDTO> lstTeam = new List<TeamDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                var query = from Team in context.TEAM_LIST
                            select Team;

                foreach (var item in query)
                {
                    item.GROUNDReference.Load();//Lazy loading :(                            
                    TeamDTO dtoTeam = new TeamDTO();

                    dtoTeam = Mapper.Map<TEAM, TeamDTO>(item);
                    lstTeam.Add(dtoTeam);
                }
                return lstTeam;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewTeam(TeamDTO dtoTeam)
        {
            try
            {
                TEAM obj = new TEAM();
                //Mapping                
                obj = Mapper.Map<TeamDTO, TEAM>(dtoTeam);

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                obj.GROUND = context.GROUND_LIST.First(g => g.GROUND_ID == dtoTeam.GROUND.GROUND_ID);

                context.AddToTEAM_LIST(obj);
                context.SaveChanges(true);

                dtoTeam.TEAM_ID = obj.TEAM_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateTeam(TeamDTO dtoTeam)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                TEAM Team = context.TEAM_LIST.First(t => t.TEAM_ID == dtoTeam.TEAM_ID);

                Team.TEAM_NAME = dtoTeam.TEAM_NAME;
                Team.TEAM_ADDRESS = dtoTeam.TEAM_ADDRESS;
                Team.TEAM_PICTURE = dtoTeam.TEAM_PICTURE;

                //Update Ground
                Team.GROUNDReference.Load();
                if (dtoTeam.GROUND.GROUND_ID != Team.GROUND.GROUND_ID)//Nếu sân mới khác sân cũ
                {
                    Team.GROUND = context.GROUND_LIST.First(g => g.GROUND_ID == dtoTeam.GROUND.GROUND_ID);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteATeam(TeamDTO dtoTeam)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                TEAM Team = context.TEAM_LIST.First(g => g.TEAM_ID == dtoTeam.TEAM_ID);
                context.DeleteObject(Team);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllTeam()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from team in context.TEAM_LIST
                             select team);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }


        public static TeamDTO FindTeam(TeamDTO dtoTeam, string searchOption)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                IQueryable<TEAM> query = null;
                if (searchOption == TeamColumns.TEAM_ID)
                {
                    query = from Team in context.TEAM_LIST
                            where Team.TEAM_ID == dtoTeam.TEAM_ID
                            select Team;

                }
                else if (searchOption == TeamColumns.TEAM_NAME)
                {
                    query = from Team in context.TEAM_LIST
                            where Team.TEAM_NAME == dtoTeam.TEAM_NAME
                            select Team;
                }

                if (query == null)
                {
                    logger.Debug("Chưa hỗ trợ tìm kiếm theo " + searchOption);
                    return null;
                }

                if (query.Count() == 0)
                    return null;

                TeamDTO Result = new TeamDTO();
                TEAM team = query.First();

                team.GROUNDReference.Load();//Lazy Loading                
                Result = Mapper.Map<TEAM, TeamDTO>(team);
                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }
    }
}
